// import axios from 'axios'
let wx_api = process.env.WX_LOGIN_API;

let userId = undefined;
let userPage = undefined;
let userValue = undefined;
let userType = undefined;

const wxLogin = async (params) => {
    return await new Promise((resolve, reject) => {
        axios.get(wx_api + '/login',params).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        });
    });
}

const delUsers = async (arr) => {
    return await new Promise((resolve, reject) => {
        axios.post('/index/user/delIds', {
            ids: arr
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        });
    });
}

const getUserDetails = async (id) => {
    userId = id;
    return await new Promise((resolve, reject) => {
        axios.get("/index/user/userDetail?id=" + id).then(res => {
            resolve(res.data.data)
        }).catch(err => {
            reject(err)
        })
    });
}

const sendEditUserRequest = async (data) => {
    return await new Promise((resolve, reject) => {
        axios.post("/index/user/saveUser?id=" + userId, data).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

const getTableData = async function (params) {
    if (params.pageNumber !== undefined) {
        userPage = params.pageNumber
    }
    if (params.fieldName !== undefined) {
        userType = params.fieldName
    } else {
        params.fieldName = userType
    }
    if (params.fieldValue !== undefined) {
        userValue = params.fieldValue
    } else {
        params.fieldValue = userValue
    }
    params.pageNumber = params.pageNumber || userPage;
    params.pageSize = 12;
    return new Promise((resolve, reject) => {
        axios.get("/index/user/workerList", {
            params
        }).then(
            res => {
                resolve(res.data.data);
            }).catch(
            err => {
                reject(err);
            }
        );
    });
}

const createUser = async (params) => {
    return await new Promise((resolve, reject) => {
        axios.post(
            "/index/user/createUser",
            params).then(
            res => {
                resolve(res)
            }).catch(
            err => {
                reject(err)
            }
        );
    })
}

const importExcel = async (params) => {
    return await new Promise((resolve, reject) => {
        axios.post('/index/user/excelData', params).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

export default {
    wxLogin,
    delUsers,
    getUserDetails,
    sendEditUserRequest,
    getTableData,
    createUser,
    importExcel
}
